$(function () {
    let layer = layui.layer;
    let form = layui.form;

    function artList() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: res => {
                const {
                    status,
                    message,
                    data
                } = res;
                if (status !== 0) return layer.msg(message);
                let str = template("tpl-table", res);
                $("tbody").html(str);
            }
        })
    };
    artList();
    let indexadd;
    $(".art-add").click(function () {
        indexadd = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '添加文章分类',
            content: $("#art-add").html()
        });
    });
    // 添加
    $('body').on("submit", "#form-add", function (e) {
        e.preventDefault();
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize(),
            success: res => {
                const {
                    message,
                    status
                } = res;
                if (status !== 0) return layer.msg(message);
                layer.msg(message)
                artList();
                layer.close(indexadd);
            }
        })
    });
    let indexedit;
    $("tbody").on("click", ".art-edit", function () {
        indexedit = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '添加文章分类',
            content: $("#art-edit").html()
        });
        let id = $(this).attr("data-id");
        $.ajax({
            method: 'GET',
            url: '/my/article/cates/' + id,
            success: res => {
                console.log(res.data);
                form.val("form--edit", res.data[0]);
            }
        })
    });
    // 编辑
    $('body').on("submit", "#form-edit", function (e) {
        e.preventDefault();
        $.ajax({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: res => {
                const {
                    message,
                    status
                } = res;
                if (status !== 0) return layer.msg(message);
                layer.msg(message)
                layer.close(indexedit);
                artList();
            }
        })
    });
    $('tbody').on('click', '.art-del', function () {
        let id = $(this).attr('data-id')
        // 提示用户是否要删除
        layer.confirm('确认删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/deletecate/' + id,
                success: function (res) {
                    if (res.status !== 0) return layer.msg(res.message);
                    layer.msg(res.message)
                    layer.close(index)
                    artList();
                }
            })
        })
    })

})